package cn.dankal.flyaudio.friend;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;

import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import api.UserServiceFactory;
import cn.dankal.basiclib.base.activity.BaseActivity;
import cn.dankal.basiclib.base.recyclerview.OnRvItemClickListener;
import cn.dankal.basiclib.pojo.MessageCenterEntity;
import cn.dankal.basiclib.pojo.MessageEntity;
import cn.dankal.basiclib.rx.AbstractSubscriber;
import cn.dankal.flyaudio.R;
import cn.dankal.flyaudio.adapter.MessageCenterAdapter;

public class MessageCenterActivity extends BaseActivity implements View.OnClickListener {

    private TextView messageSystemContentView;
    private TextView messageCommentContentView;
    private TextView messageFlowContentView;
    private TextView messageYuyueContentView;
    private TextView messageScoresContentView;

    private TextView messageSystemTimeView;
    private TextView messageCommentTimeView;
    private TextView messageFlowTimeView;
    private TextView messageYuyueTimeView;
    private TextView messageScoresTimeView;

    private View messageSystemView;
    private View messageCommentView;
    private View messageFlowView;
    private View messageYuyueView;
    private View messageScoresView;

    private MessageEntity lastSystemMessage;
    private MessageEntity lastCommentMessage;
    private MessageEntity lastFlowMessage;
    private MessageEntity lastYuyueMessage;
    private MessageEntity lastScoresMessage;

    private RecyclerView messageList;
    private MessageCenterAdapter adapter;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_message_center;
    }

    @Override
    protected void initComponents() {
        addSingleTitleBar("消息中心");
        messageList = findViewById(R.id.recyclerview_message);
        messageList.setLayoutManager(new LinearLayoutManager(this));
        adapter = new MessageCenterAdapter();
        messageList.setAdapter(adapter);
//        adapter.setOnRvItemClickListener(new OnRvItemClickListener<MessageEntity>() {
//            @Override
//            public void onItemClick(View v, int position, MessageEntity data) {
//                Intent intent = new Intent(MessageCenterActivity.this, MessageDetailActivity.class);
//                intent.putExtra("message", data);
//                startActivity(intent);
//            }
//        });
//        messageSystemContentView = findViewById(R.id.tv_system_message);
//        messageCommentContentView = findViewById(R.id.tv_message_comment);
//        messageFlowContentView = findViewById(R.id.tv_message_flow);
//        messageYuyueContentView = findViewById(R.id.tv_message_yuyue);
//        messageScoresContentView = findViewById(R.id.tv_message_scores);
//
//        messageSystemTimeView = findViewById(R.id.tv_system_time);
//        messageCommentTimeView = findViewById(R.id.tv_message_comment_time);
//        messageFlowTimeView = findViewById(R.id.tv_message_flow_time);
//        messageYuyueTimeView = findViewById(R.id.tv_message_yuyue_time);
//        messageScoresTimeView = findViewById(R.id.tv_message_scores_time);
//
//        messageSystemView = findViewById(R.id.rl_system_message);
//        messageCommentView = findViewById(R.id.rl_message_comment);
//        messageFlowView = findViewById(R.id.rl_message_flow);
//        messageYuyueView = findViewById(R.id.rl_message_yuyue);
//        messageScoresView = findViewById(R.id.rl_message_scores);
//
//        messageSystemView.setOnClickListener(this);
//        messageCommentView.setOnClickListener(this);
//        messageFlowView.setOnClickListener(this);
//        messageYuyueView.setOnClickListener(this);
//        messageScoresView.setOnClickListener(this);
//

        fetMessage();
    }

    private void fetMessage() {
        UserServiceFactory.messageCenter().subscribe(new AbstractSubscriber<MessageCenterEntity>(this) {
            @Override
            public void onNext(MessageCenterEntity messageCenterEntity) {
                Log.d("MessageCenter", JSON.toJSONString(messageCenterEntity));
                List<MessageEntity> systemMessage = messageCenterEntity.getSystemMessage();
                List<MessageEntity> appMessage = messageCenterEntity.getAppMessage();
                List<MessageEntity> commentMessage = messageCenterEntity.getCommentMessage();
                List<MessageEntity> scoreMessage = messageCenterEntity.getScoreMessage();
                List<MessageEntity> followMessage = messageCenterEntity.getFollowMessage();
//                if (systemMessage != null && !systemMessage.isEmpty()) {
//                    adapter.addDatas(systemMessage, true);
//                }
//                if (appMessage != null && !appMessage.isEmpty()) {
//                    adapter.addDatas(appMessage, false);
//                }
//                if (commentMessage != null && !commentMessage.isEmpty()) {
//                    adapter.addDatas(commentMessage, false);
//                }
//                if (scoreMessage != null && !scoreMessage.isEmpty()) {
//                    adapter.addDatas(scoreMessage, false);
//                }
//                if (followMessage != null && !followMessage.isEmpty()) {
//                    adapter.addDatas(followMessage, false);
//                }
                adapter.sortByTime();
//
//                if (systemMessage == null || systemMessage.isEmpty()) {
//                    messageSystemView.setVisibility(View.GONE);
//                } else {
//                    lastSystemMessage = systemMessage.get(0);
//                    messageSystemContentView.setText(systemMessage.get(0).getContent());
//                    messageSystemTimeView.setText(systemMessage.get(0).getContent());
//                }
//                if (appMessage == null || appMessage.isEmpty()) {
//                    messageYuyueView.setVisibility(View.GONE);
//                } else {
//                    lastYuyueMessage = appMessage.get(0);
//                    messageYuyueContentView.setText(appMessage.get(0).getContent());
//                    messageYuyueTimeView.setText(appMessage.get(0).getContent());
//                }
//                if (commentMessage == null || commentMessage.isEmpty()) {
//                    messageCommentView.setVisibility(View.GONE);
//                } else {
//                    lastCommentMessage = commentMessage.get(0);
//                    messageCommentContentView.setText(commentMessage.get(0).getContent());
//                    messageCommentTimeView.setText(commentMessage.get(0).getContent());
//                }
//                if (followMessage == null || followMessage.isEmpty()) {
//                    messageFlowView.setVisibility(View.GONE);
//                } else {
//                    lastFlowMessage = followMessage.get(0);
//                    messageFlowContentView.setText(followMessage.get(0).getContent());
//                    messageFlowTimeView.setText(followMessage.get(0).getContent());
//                }
//                if (scoreMessage == null || scoreMessage.isEmpty()) {
//                    messageScoresView.setVisibility(View.GONE);
//                } else {
//                    lastScoresMessage = scoreMessage.get(0);
//                    messageScoresContentView.setText(scoreMessage.get(0).getContent());
//                    messageScoresTimeView.setText(scoreMessage.get(0).getContent());
//                }
            }
        });
    }


    @Override
    public void onClick(View v) {
//        int id = v.getId();
//        Intent intent = new Intent(this, MessageDetailActivity.class);
//        switch (id) {
//            case R.id.rl_system_message:
//                intent.putExtra("message", lastSystemMessage);
//                break;
//            case R.id.rl_message_comment:
//                intent.putExtra("message", lastCommentMessage);
//                break;
//            case R.id.rl_message_flow:
//                intent.putExtra("message", lastFlowMessage);
//                break;
//            case R.id.rl_message_yuyue:
//                intent.putExtra("message", lastYuyueMessage);
//                break;
//            case R.id.rl_message_scores:
//                intent.putExtra("message", lastScoresMessage);
//                break;
//        }
//        startActivity(intent);
    }
}
